'use strict'

import PostService from './PostService.js'

class PostController {

    async create(reg, res) {
        try {
            const post = await PostService.create(reg.body, reg.files.picture)
            res.json(post)
        } catch (error) {
            res.status(500).json(error)
        }
    }

    async getAll(reg, res) {
        try {
            const posts = await PostService.getAll()
            return res.json(posts)
        } catch (error) {
            res.status(500).json(error)
        }
    }

    async getOne(reg, res) {
        try {
            const post = await PostService.getOne(reg.params.id)
            return res.json(post)
        } catch (error) {
            res.status(500).json(error)
        }
    }

    async update(reg, res) {
        try {
            const updatedPost = await PostService.update(reg.body)
            return res.json(updatedPost)
        } catch (error) {
            res.status(500).json(error.message)
        }
    }

    async delete(reg, res) {
        try {
            const post = await PostService.delete(reg.params.id)
            return res.json(post)
        } catch (error) {
            res.status(500).json(error)
        }
    }

    async deleteAll(reg, res) {
        try {
            const posts = await PostService.deleteAll()
            return res.json('all posts deleted')
        } catch (error) {
            res.status(500).json(error)
        }
    }

}

export default new PostController()